/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.univaq.f4i.iw.ex.album.model.impl;

import it.univaq.f4i.iw.ex.album.model.Guest;
import it.univaq.f4i.iw.ex.album.model.Invitation;
import it.univaq.f4i.iw.ex.album.model.Manager;
import it.univaq.f4i.iw.ex.album.model.MeetingDataLayer;
import it.univaq.f4i.iw.ex.album.model.MeetingRoom;
import it.univaq.f4i.iw.ex.album.model.Organizer;
import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author juli
 */
public class MeetingDataLayerMysqlImpl implements MeetingDataLayer {
    
    private PreparedStatement gManager,dManager, gOrganizer, dOrganizer, gGuest, dGuest, gMeetingRoom, getInvi, dInvitation, uMeetingRoom, uInvi;
    private String basepath_image, basepath_thumbnail;
    
    public MeetingDataLayerMysqlImpl(String basepath_image, String basepath_thumbnail, Connection connection) throws SQLException{
         this.basepath_image = basepath_image;
        this.basepath_thumbnail = basepath_thumbnail;

        //precompiliamo tutte le query utilizzate nella classe
        //precompile all the queries uses in this class
        gManager = connection.prepareStatement("SELECT * FROM manager WHERE ID=?");
        gOrganizer = connection.prepareStatement("SELECT * FROM organizer WHERE ID=?");
        gGuest = connection.prepareStatement("SELECT * FROM guest WHERE ID=?");
        gMeetingRoom = connection.prepareStatement("SELECT * FROM meetingroom WHERE  ID=?");
        getInvi = connection.prepareStatement("SELECT * FROM invitation WHERE ID=?");
        //notare l'ultimo paametro extra di questa chiamata a
        //prepareStatement: lo usiamo per assicurarci che il JDBC
        //restituisca la chiave generata automaticamente per il
        //record inserito

        //note the last parameter in this call to prepareStatement:
        //it is used to ensure that the JDBC will sotre and return
        //the auto generated key for the inserted recors
        uMeetingRoom = connection.prepareStatement("UPDATE meetingroom SET name=?,place=?,capacity=?,properties=? WHERE ID=?");
        dManager = connection.prepareStatement("DELETE FROM manager WHERE ID=?");
        dOrganizer = connection.prepareStatement("DELETE FROM organizer WHERE ID=?");
        dGuest = connection.prepareStatement("DELETE FROM guest WHERE ID=?");
        dInvitation = connection.prepareStatement("DELETE FROM invitation WHERE ID=?");
        uInvi = connection.prepareStatement("UPDATE foto SET key_org=?,guests=?,key_mr=?, date=? WHERE ID=?");
    }

    public Manager createManager() {
        return new ManagerMysqlImpl(this);
    } //done

    public Manager deleteManager(Manager man) {
           try {
            dManager.setInt(1, man.getKey());
           
            if (dManager.executeUpdate() == 1) {
                dManager.execute();
                return man;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MeetingDataLayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    } //done


    public Organizer createOrganizer() {
        return new OrganizerMysqlImpl(this);
    } //done

    public Organizer deleteOrganizer(Organizer org) {
         try {
            dOrganizer.setInt(1, org.getKey());
           
            if (dOrganizer.executeUpdate() == 1) {
                dOrganizer.execute();
                return org;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MeetingDataLayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    } //done

    public Guest createGuest() {
        return new GuestMysqlImpl(this);
    } //done

    public Guest deleteGuest(Guest gue) {
            try {
            dGuest.setInt(1, gue.getKey());
           
            if (dGuest.executeUpdate() == 1) {
                dGuest.execute();
                return gue;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MeetingDataLayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    } //done

    public MeetingRoom createMeetingRoom() {
        return new MeetingRoomMysqlImpl(this);
    } //done

    public Invitation deteleInvitation(Invitation inv) {
            try {
            dInvitation.setInt(1, inv.getKey());
           
            if (dInvitation.executeUpdate() == 1) {
                dInvitation.execute();
                return inv;
            }
        } catch (SQLException ex) {
            Logger.getLogger(MeetingDataLayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    } //done

    public List<String> getPropertiesMeetingRoom(MeetingRoom m) {
        //tonteria meetingrm tiene ya propiedades
        return m.getProperties();
    }

    public List<String> addProperty(MeetingRoom m, List<String> properties) {
         try {
            gMeetingRoom.setInt(1, m.getKey());
           
            if (dGuest.executeUpdate() == 1) {
                MeetingRoom met = gMeetingRoom.executeQuery().getObject(0, MeetingRoom.class);
                met.setProperties(properties);
                uMeetingRoom.setInt(1, met.getKey());
                uMeetingRoom.setArray(5, (Array) met.getProperties());
                uMeetingRoom.execute();
                return met.getProperties();
            }
        } catch (SQLException ex) {
            Logger.getLogger(MeetingDataLayer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    } //done 

    public List<String> deleteProperty(MeetingRoom m, List<String> properties) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public List<Guest> addGuest(Invitation i, List<Guest> g) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public List<Guest> deleteGuest(Invitation i, List<Guest> g) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public Organizer getOrganizer(String key_org) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public List<Guest> getGuestByInvitation(Invitation i) {
        //mismo que meeting xD
        return i.getGuests();
    }

    public MeetingRoom getMeetingRoom(int key_mr) {
        
        try {
            gMeetingRoom.setInt(1 , key_mr);
             if (gMeetingRoom.executeUpdate() == 1) {
                return gMeetingRoom.executeQuery().getObject(0, MeetingRoomMysqlImpl.class);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(MeetingDataLayerMysqlImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    } //PREGUNTAR SI DE UNA BUSQUEDA PUEDO SACAR EL OBJETO
    public Invitation createInvitation(Organizer o, MeetingRoom m, List<Guest> l, String date, String startTime, String endTime) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public Invitation setInvitation(Integer key, List<Guest> l, String date, String startTime, String endTime) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
